package com.gwz.controller.admin;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.gwz.context.ProContent;
import com.gwz.model.Admin;
import com.gwz.service.admin.AdminService;


/**
 * 
 * @author panguixiang
 * 登录quartz平台控制器
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 登录页面
     * @param model
     * @return
     */
    @RequestMapping(value = "init", method = RequestMethod.GET)
    public String login() {
        return "/admin/login";
    }

    /**
     * 登录
     * @param model
     * @return
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String login(@RequestParam("account") String account,
                        @RequestParam("password") String password, Model model,
                        HttpServletRequest request) {
    	Admin admin = adminService.login(account.trim(), password.trim());
        if (admin!=null) {
            request.getSession(true).setAttribute(ProContent.LOGIN_SESSION, admin);
            return "/admin/quartzList";
        } else {
            model.addAttribute("loginMsg", "登录账号或密码错误，或不是koko后台超级管理员");
            return "/admin/login";
        }
    }

    /**
     * 退出登录
     * @param model
     * @return
     */
    @RequestMapping(value = "loginout", method = RequestMethod.GET)
    public String out(HttpServletRequest request) {
        request.getSession(true).invalidate();
        return "redirect:/admin/init";
    }

}
